new Vue({
  el: "#app",
  data() {
    return {
      Contract: [],
      enterprise_info: [],
      multipleSelection: [],
      ye: 1, //绑定初始页数
      Total: 0, //绑定初始条数
      bTotal: 0,
      gotal: 0,
      ccTotal: 0,
      sjTotal: 0,
      // jytype: false,
      BASE_IMG: "https://www.kdddy.cn/Image/",
      activeName: "first",
      UserID: decodeURI($.cookie("use")),
      form: {
        // SQLSerialNumber: "P_business_category",
        PaginationNumber: 1, //页数
        RowAmount: 15, //每页数据条数
        Category_Name: "",
        Condition: "",
        ExtensionCondition: "",
      },
      purchase: [],
      wdbj: [],
      wdgy: [],
      sortProps: [],
      currentSort: null
    };
  },
  mounted() {
    this.ccgd(this.ye);
    this.bjd(this.ye);
    this.gyd(this.ye);
    this.enter("", this.ye);
    this.jiaoy(this.ye);
  },
  methods: {
    show(row){
      window.location.href ='./show.html?Enterprise_Info_SerialNumber=' + row.Enterprise_Info_SerialNumber;
    },
    handleSortChange({ column, prop, order }) {
      this.currentSort = { prop, order };
      this.updateSortProps(prop, order);
      this.sortTableData();
      this.setSortIndicator();
    },
    
    updateSortProps(prop, order) {
      this.sortProps = this.sortProps.filter(item => item.prop !== prop);
      if (order) {
        this.sortProps.unshift({ prop, order });
      }
    },
    
    sortTableData() {
      if (this.sortProps.length === 0) {
        this.enterprise_info = [...this.originalData];
        return;
      }
      
      const data = [...this.enterprise_info];
      
      data.sort((a, b) => {
        for (const sort of this.sortProps) {
          const { prop, order } = sort;
          const aValue = a[prop];
          const bValue = b[prop];
          
          if (aValue !== bValue) {
            return order === 'ascending' 
              ? this.compare(aValue, bValue)
              : this.compare(bValue, aValue);
          }
        }
        return 0;
      });
      
      this.enterprise_info = data;
    },
    
    compare(a, b) {
      if (typeof a === 'number' && typeof b === 'number') {
        return a - b;
      }
      return String(a).localeCompare(String(b));
    },
    
    sortByMultiple(row) {
      return this.sortProps.map(sort => row[sort.prop]);
    },
    
    setSortIndicator() {
      this.$refs.table.columns.forEach(column => {
        column.order = null;
      });
      
      if (this.currentSort) {
        const column = this.$refs.table.columns.find(
          col => col.property === this.currentSort.prop
        );
        if (column) {
          column.order = this.currentSort.order;
        }
      }
    },
    jyf(res) {
      console.log(res);
      this.ye = res; //修改当前页
      this.jiaoy(res); //调用接口传给后端
    },
    jiaoy(res) {
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        type: "POST",
        url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "https://service.kdddy.cn/donghui/login/generalQueryByTableNameAndPaginationForClient",
        data: {
          SQLSerialNumber: "A_business_Contract", //表名
          Condition: "",

          ExtensionCondition:'',
          PaginationNumber: res,
          RowAmount: 11,
        },
        success: function (Result) {
          console.log(Result);
          _this.Total = Result.TotalRows;
          _this.Contract = Result.Rows;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handledownload(row){
      console.log(row.picture);
      if (row.picture) {
        row.picture.split(';').map(s => 'https://www.kdddy.cn/Image/' + s).forEach(item=>{
          window.location.href = item;
        })
      }
    },
    shanchu(row){
      row.Row_Status = '99'
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        type: "POST",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: `${BASE_URL2}/generalBusiness/generalSave`,
        data: {
          Data: JSON.stringify({Modify:{
            enterprise_info:[row]
          }})
        },
        success: function (Result) {
          console.log(Result);
          _this.enter("", _this.ye);
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
      console.log(row);
    },
    xiugai(row){
      sessionStorage.setItem("Enterprise_Info_SerialNumber",row.Enterprise_Info_SerialNumber)
      window.location.href = 'edit1.html?Enterprise_Info_SerialNumber=' + row.Enterprise_Info_SerialNumber
    
    },
    isButtonEnabled(row) {
      if (row.Need_Review == "1") return true;
    },
    ybsahng(index, row) {
      window.location.href = "./shenhe.html";
    },
    shgn(index, row) {
      console.log(row);
      if (row.Need_Review == "0") {
        row.Need_Review = "1";
      } else {
        row.Need_Review = "0";
      }

      let lArray = new Array();
      lArray.push(row);
      let lJSONObject_enterprise_info = new Object();
      lJSONObject_enterprise_info.enterprise_info = lArray; //换表
      let lJSONObject_DataSet = new Object(); // Modify
      lJSONObject_DataSet.Modify = lJSONObject_enterprise_info; //换表
      const _this = this;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message({
              message: "提交成功",
              type: "success",
            });
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    enter(data, res) {
      console.log(data);
      if (data == undefined) {
        data = "";
      }
      const _this = this;
      // A_enterprise_info
      $.ajax({
        headers: requestHeaders(),
        type: "POST",
        url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumberWithOriginal`,
        // url: "https://service.kdddy.cn/donghui/login/generalQueryByTableNameAndPaginationForClient",
        data: {
          SQLSerialNumber: "A_enterprise_info", //表名
          Condition: window.btoa(unescape(encodeURIComponent("%"+data+"%"))),
          // PaginationNumber: res,
          // RowAmount: 15,
          ExtensionCondition:'',
        },
        success: function (Result) {

          _this.enterprise_info = Result;
          _this.sjTotal = Result.length;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    ghandleCurrentChange(arg) {
      this.gyd(arg);
    },
    bhandleCurrentChange(arg) {
      this.bjd(arg);
    },
    jytype(row) {
      if (row.Row_Status == "1") return true;
    },
    filterTag2(value, row) {
      return row.Row_Status === value;
    },
    filterTag3(value, row) {
      return row.Need_Review === value;
    },

    cyhandleClick(tab, event) {
      console.log(tab, event);
    },
    gyshanchu(index, row) {
      console.log("row:", row, "index:", index);
      const _this = this;
      console.log("row:", row);
      console.log(row.Row_Status);
      row.Row_Status = "1";
      console.log("row:", row);
      delete row.Operation_Datetime;
      delete row.Operation_Datetime_Original;
      delete row.Operation_ID;
      delete row.Operation_ID_Original;
      let lArray = new Array();
      lArray.push(row);
      let lJSONObject_business_Quotation = new Object();
      lJSONObject_business_Quotation.business_Quotation = lArray; //换表
      let lJSONObject_DataSet = new Object();
      lJSONObject_DataSet.Modify = lJSONObject_business_Quotation;
      console.log(lJSONObject_DataSet);
      console.log(index);

      // return;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status == "success") {
            _this.$confirm("此操作将永久删除, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            });
            //   .then(() => {
            //     _this.$message({
            //       type: "success",
            //       message: "删除成功!",
            //     });
            //   }
            // )
            //   .catch(() => {
            //     _this.$message({
            //       type: "info",
            //       message: "已取消删除",
            //     });
            //   });

            _this.wdgy.splice(index, 1);
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    cgshanchu(index, row) {
      row.Row_Status = "1";
      delete row.Operation_Datetime;
      delete row.Operation_Datetime_Original;
      let lArray = new Array();
      lArray.push(row);
      let lJSONObject_business_PurchaseRequest = new Object();
      lJSONObject_business_PurchaseRequest.business_PurchaseRequest = lArray; //换表
      let lJSONObject_DataSet = new Object();
      lJSONObject_DataSet.Modify = lJSONObject_business_PurchaseRequest;
      console.log(lJSONObject_DataSet);
      console.log(index);
      const _this = this;
      // return;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status == "success") {
            // _this.purchase.splice(index, 1);
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    bjshanchu(index, row) {
      console.log("row:", row, "index:", index);
      const _this = this;
      console.log("row:", row);
      console.log(row.Row_Status);
      row.Row_Status = "1";
      console.log("row:", row);
      delete row.QuotationDate;
      delete row.QuotationDate_Original;
      delete row.Operation_Datetime;
      delete row.Operation_Datetime_Original;
      delete row.Operation_ID;
      delete row.Operation_ID_Original;
      let lArray = new Array();
      lArray.push(row);
      let lJSONObject_business_Quotation = new Object();
      lJSONObject_business_Quotation.business_Quotation = lArray; //换表
      let lJSONObject_DataSet = new Object();
      lJSONObject_DataSet.Modify = lJSONObject_business_Quotation;
      console.log(lJSONObject_DataSet);
      console.log(index);

      // return;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status == "success") {
            // _this.wdbj.splice(index, 1);
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    cgshan(index, row) {
      const _this = this;
      console.log("row:", row);
      console.log(row.Row_Status);
      row.Row_Status = "1";
      console.log("row:", row);
      delete row.Operation_Datetime;
      delete row.Operation_Datetime_Original;
      delete row.Operation_ID;
      delete row.Operation_ID_Original;
      let lArray = new Array();
      lArray.push(row);
      let lJSONObject_business_PurchaseRequest = new Object();
      lJSONObject_business_PurchaseRequest.business_PurchaseRequest = lArray; //换表
      let lJSONObject_DataSet = new Object();
      lJSONObject_DataSet.Modify = lJSONObject_business_PurchaseRequest;
      console.log(lJSONObject_DataSet);
      console.log(index);

      // return;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status == "success") {
            _this.$confirm("此操作将永久删除, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            });
            //   .then(() => {
            //     _this.$message({
            //       type: "success",
            //       message: "删除成功!",
            //     });
            //   }
            // )
            //   .catch(() => {
            //     _this.$message({
            //       type: "info",
            //       message: "已取消删除",
            //     });
            //   });

            _this.purchase.splice(index, 1);
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    //查询个人公司全部供应单
    gyd(arg) {
      let Enterprise_Info_SerialNumber = sessionStorage.getItem(
        "Enterprise_Info_SerialNumber"
      );
      console.log(Enterprise_Info_SerialNumber);
      const _this = this;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "https://service.kdddy.cn/donghuiForRequest/selectByData/selectEnterpriseID",
        // url: "https://service.kdddy.cn/donghuiForRequest/selectByData/PersonBusinessQuotation",
        // url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableName",
        url: "https://service.kdddy.cn/donghui/clientOperation/getProvideListByEnterprise_SerialNumber",
        type: "get",
        data: {
          Enterprise_SerialNumber: Enterprise_Info_SerialNumber,
          status: "1",
          PageNubmer: arg,
          RowAmount: 15,
        },
        success: function (Result) {
          console.log(Result);
          _this.wdgy = Result.Rows;
          _this.gotal = Result.Total;
        },
        complete: function (XMLHttpRequest) {
          // requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    handleClick(row) {
      console.log(row);
    },
    // 查询个人公司全部报价单
    bjd(arg) {
      let Enterprise_Info_SerialNumber = sessionStorage.getItem(
        "Enterprise_Info_SerialNumber"
      );
      console.log(Enterprise_Info_SerialNumber);
      const _this = this;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "https://service.kdddy.cn/donghuiForRequest/selectByData/selectEnterpriseID",
        // url: "https://service.kdddy.cn/donghuiForRequest/selectByData/PersonBusinessQuotation",
        url: "https://service.kdddy.cn/donghui/clientOperation/getProvideListByEnterprise_SerialNumber",
        // url: "http://192.168.10.66:8083/clientOperation/getProvideListByEnterprise_SerialNumber",
        type: "get",
        // async: false,
        // async: true,
        data: {
          Enterprise_SerialNumber: Enterprise_Info_SerialNumber,
          status: "0",
          PageNubmer: arg,
          RowAmount: 10,
        },
        success: function (Result) {
          console.log(Result);
          _this.wdbj = Result.Rows;
          _this.bTotal = Result.Total;
          // for (let i = 0; i < _this.wdbj.length; i++) {
          //   if (_this.wdbj[i].ConfirmationStatus == "0") {
          //     _this.wdbj[i].ConfirmationStatus = "未签约";
          //     console.log(_this.wdbj);
          //   } else {
          //     _this.wdbj[i].ConfirmationStatus = "已签约";
          //     console.log(_this.wdbj[i].ConfirmationStatus);
          //   }
          // }
        },
        complete: function (XMLHttpRequest) {
          // requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    // 根据企业ID去查求购单
    ccgd(res) {
      console.log(res, "ccgdres");
      let Enterprise_Info_SerialNumber = sessionStorage.getItem(
        "Enterprise_Info_SerialNumber"
      );
      console.log(Enterprise_Info_SerialNumber);

      const _this = this;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        url: "https://service.kdddy.cn/donghui/clientOperation/getPurchaseRequestListByEnterprise_SerialNumber",
        type: "get",
        data: {
          Enterprise_SerialNumber: Enterprise_Info_SerialNumber,
          PageNubmer: res, //页
          RowAmount: 15, //条数
        },
        success: function (Result) {
          console.log(Result);
          _this.purchase = Result.Rows;
          _this.ccTotal = Result.Total;
        },
        complete: function (XMLHttpRequest) {
          // requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    // 通过用户编号获得对应的企业编号
    // getRequesterID(Arg_UserID) {
    //   let Enterprise_Info_SerialNumber = sessionStorage.getItem(
    //     "Enterprise_Info_SerialNumber"
    //   );
    //   const _this = this;
    //   $.ajax({
    //     headers: {
    //       AccessToken: $.cookie("token"), //会将数据转换为query string
    //     },
    //     // headers: requestHeaders(),
    //     // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
    //     url: "https://service.kdddy.cn/donghuiForRequest/selectByData/selectEnterpriseID",
    //     type: "get",
    //     // async: false,
    //     // async: true,
    //     data: {
    //       Data: Enterprise_Info_SerialNumber,
    //     },
    //     success: function (Result) {
    //       console.log(Result);
    //       console.log(Result.Data[0].Enterprise_Info_SerialNumber);
    //       let Enterprise_Info_SerialNumber =
    //         Result.Data[0].Enterprise_Info_SerialNumber;
    //       sessionStorage.setItem(
    //         "Enterprise_Info_SerialNumber",
    //         Enterprise_Info_SerialNumber
    //       );
    //       _this.ccgd(Result.Data[0].Enterprise_Info_SerialNumber);
    //     },
    //     complete: function (XMLHttpRequest) {
    //       requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
    //     },
    //     dataType: "json",
    //   });
    // },
    filterTag(value, row) {
      // console.log(value, row);

      return row.ConfirmationStatus === value;
    },
    filterTag4(value, row) {
      return row.Acceptance_Status === value;
    },
    onSerach() {
      console.log(this.form.SQL_SerialNumber);
      this.enter(this.form.SQL_SerialNumber, this.ye);
      // //查询方法
      // this.form.PaginationNumber = 1;
      // this.getList(this.form);
    },
    Add() {
      window.location.href = "./add.html";
    },
    handleSizeChange(val) {
      this.form.PaginationNumber = 1;
      this.form.RowAmount = val;
      this.getList(this.form);
    },
    handleCurrentChange(val) {
      console.log(val);
      this.ye = val; //修改当前页
      this.ccgd(val);
    },
    sjhandleCurrentChange(val) {
      console.log(val);
      this.ye = val; //修改当前页
      this.enter("", val);
    },
    Edit(Category_SerialNumber) {
      sessionStorage.setItem("Category_SerialNumber", Category_SerialNumber);
      window.location.href =
        "./edit.html?Category_SerialNumber=" + Category_SerialNumber;
    },
    Remove(row) {
      const that = this;
      this.$confirm("警告！此操作将删除该类别, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          row.Row_Status = "1";
          $.ajax({
            headers: requestHeaders(),
            url: `${BASE_URL2}/generalBusiness/generalSave`,
            type: "POST",
            data: {
              Data: JSON.stringify({ Modify: { business_category: [row] } }),
            },
            success: function (res) {
              if (res.Status === "success") {
                that.$message({
                  type: "success",
                  message: "删除成功!",
                });
                that.getList(that.form);
              } else {
                that.$message.error("删除失败，请稍后再试");
              }
            },
            complete: function (XMLHttpRequest) {
              that.loading = false;
              requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
            },
            dataType: "json",
          });
        })
        .catch(() => {});
    },
  },
});
